package com.pingan.generator.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.pingan.generator.api.entity.ColInfo;
import com.pingan.generator.api.entity.mapper.ColInfoMapper;
import com.pingan.generator.api.entity.util.GetFile;
import com.pingan.generator.api.service.InterFaceService;
import com.pingan.generator.api.tool.CollectionTool;

@Service("interFaceService")
public class InterFaceServiceImpl implements InterFaceService {

	@Autowired
	private ColInfoMapper colMappper;

	String url = "/appVersion/removePushInfoInfoByKey";
	String clazz = "com.github.pagehelper.PageInfo<com.pingan.generator.api.entity.AppVersionInfo>";

	@Override
	public String getCheck(String a, String b) {

		String describeAdd = null;
		String describeDes = null;

		Map<String, Object> map = null;

		List<ColInfo> l = null;

		// 数据库键
		List<String> oldKey = new ArrayList<String>();

		// 修改后的键
		List<String> newKey = new ArrayList<String>();

		try {
			if (clazz.contains("<")) {
//				map = GetFile.getFileds(Class.forName(clazz.substring(0, clazz.indexOf("<"))),new HashMap<String, Object>(), clazz.substring(clazz.indexOf("<") + 1, clazz.lastIndexOf(">")),"");
			} else {
			//	map = GetFile.getFileds(Class.forName(clazz), new HashMap<String, Object>(), null,"");
				map = GetFile.getFileds(Class.forName(clazz.substring(0, clazz.indexOf("<"))), new HashMap<String, Object>(),
						clazz.substring(clazz.indexOf("<") + 1, clazz.lastIndexOf(">")), "", url);
				map = GetFile.getFileds(Class.forName(clazz), new HashMap<String, Object>(), null, "", url);
			}

			oldKey = (List<String>) map.get("key");
			List ll = (List) map.get("list");
			List<String> ls = (List<String>) ll.get(ll.size() - 1);

			l = colMappper.getColNameList(url);
			// 键值储存
			for (ColInfo info : l) {
				// 如果是泛型
				if ("Y".equals(info.getIsList())) {
				} else {
					newKey.add(info.getCol());
				}
			}

			List<String> listAdd = CollectionTool.getNotContain(oldKey, newKey);

			List<String> listDes = CollectionTool.getNotContain(newKey, oldKey);

			System.out.println(listAdd);

			System.out.println(listDes);

			describeAdd = listAdd == null ? null : "新增字段为" + JSON.toJSONString(listAdd);
			describeDes = listDes == null ? null : "删减字段为" + JSON.toJSONString(listDes);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return describeAdd + describeDes;
	}

	public static void main(String[] args) {
		// new
		// InterFaceServiceImpl().getCheck("/appVersion/removePushInfoInfoByKey",
		// "com.github.pagehelper.PageInfo<com.pingan.generator.api.entity.AppVersionInfo>");
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		System.out.println(list.get(list.size() - 1));
	}

}
